﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;

namespace Server
{
    class FileThread
    {
        //private Socket fileSocket;
        //private FileHandler filehandler;
        private Socket filelistenSocket;


       public FileThread()
        {
            

        }



        //public void StartFileListening(string[] message)
        //{
        //    IPAddress[] ipAddress = Dns.GetHostAddresses(Dns.GetHostName());
        //    filelistenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        //    filelistenSocket.Bind(new IPEndPoint(ipAddress[0], 5652));

        //    filelistenSocket.Listen(100);

        //    Thread threadFile = new Thread(new ParameterizedThreadStart(fileHandle));
        //    threadFile.Name = "FileThread";
        //    object[] parra = new object[2];
            
        //    Socket fileSocket = filelistenSocket.Accept();
            
        //    parra[0] = fileSocket;
        //    parra[1] = message;
            
        //    threadFile.Start(parra);


        //}


        public void fileHandle(object Socket)
        {
            System.Diagnostics.Debug.WriteLine("File Thread has started");
            //object[] decode = (object[])objarray;
            Socket fileSocket = (Socket)Socket;
            //string[] message = (string[])decode[1];



            FileHandler filehandler = new FileHandler(fileSocket, Option.getoption());

            
            //string[] message = (string[])mess;
            Message message = new Message();
            string[] messarray = message.recMess(fileSocket).Split(',');
         
            filehandler.handleFile(messarray);

            //DisconnectFileSocket();
            //DisconnectListener();

            fileSocket.Shutdown(SocketShutdown.Both);
            fileSocket.Disconnect(true);
            fileSocket.Close();

        }

        private void DisconnectListener()
        {
            filelistenSocket.Dispose();
            filelistenSocket = null;

        }

        //private void DisconnectFileSocket()
        //{
        //    fileSocket.Shutdown(SocketShutdown.Both);
        //    fileSocket.Disconnect(true);
        //    fileSocket.Close();
        //    fileSocket = null;

        //}
    }



}

